﻿Imports BUS
Imports DTO
Public Class frmPhieuThuTien


    Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged

    End Sub

    Private Sub frmPhieuThuTien_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim danhsach As DataTable = KhachHangBUS.LayNhungKhachHangConBiNo()
        ComboBox_HoTen.DataSource = danhsach
        ComboBox_HoTen.DisplayMember = "TenKhachHang"
        ComboBox_HoTen.ValueMember = "MaKhachHang"
        ComboBox_HoTen.SelectedIndex = 0
        ComboBox_HoTen_SelectedIndexChanged(sender, e)
    End Sub

    Private Sub ComboBox_HoTen_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox_HoTen.SelectedIndexChanged, ComboBox_Email.SelectedIndexChanged, ComboBox_DienThoai.SelectedIndexChanged, ComboBox_DiaChi.SelectedIndexChanged

        If ComboBox_HoTen.SelectedIndex < 0 Then
            Return
        End If
        Dim MaKhachHang As Integer
        Try
            MaKhachHang = Integer.Parse(ComboBox_HoTen.SelectedValue)

        Catch ex As Exception
            Return
        End Try

        Dim dt As DataTable = KhachHangBUS.DoDuLieuVaoPhieuThu(MaKhachHang)
        ComboBox_DiaChi.DataSource = dt
        ComboBox_DiaChi.DisplayMember = "DiaChi"
        ComboBox_DiaChi.ValueMember = "MaKhachHang"

        ComboBox_DienThoai.DataSource = dt
        ComboBox_DienThoai.DisplayMember = "DienThoai"
        ComboBox_DienThoai.ValueMember = "MaKhachHang"

        ComboBox_Email.DataSource = dt
        ComboBox_Email.DisplayMember = "Email"
        ComboBox_Email.ValueMember = "MaKhachHang"



    End Sub

    Private Sub GroupBox2_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox2.Enter

    End Sub

    Private Sub btnLapPhieuThu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLapPhieuThu.Click
        ' Lay ra so tien ma khach hang nay no
        Dim KhachHang As BangKhachHangDTO = New BangKhachHangDTO()
        KhachHang.strTenKhachHang = ComboBox_HoTen.Text
        KhachHang.DDienThoai = Double.Parse(ComboBox_DienThoai.Text)
        KhachHang.strDiaChi = ComboBox_DiaChi.Text
        KhachHang.strEmail = ComboBox_Email.Text
        Dim sotienno = KhachHangBUS.LaySoTienKhachHangNo(KhachHang)
        ' Lay ra so tien ma khach hang thanh toan
        Dim SoTienThanhToan As Double = Double.Parse(nudSoTienThu.Value)
        If (SoTienThanhToan > sotienno) Then
            MessageBox.Show("Ban chi nợ: " + sotienno.ToString() + "Nhap lai di kia")
            Return
        End If
        Dim GhiNhanlaitienno As Double = sotienno - SoTienThanhToan
        KhachHang.dSoTienNo = GhiNhanlaitienno
        Dim kq As Double = KhachHangBUS.CapNhatLaiTienNo(KhachHang)
        ' Luu Vao Bang Phieu Thu
        Dim dtNgayThu As DateTime = DateTimePicker1.Text
        PhieuThuBUS.LapBangPhieuThu(KhachHang, dtNgayThu, SoTienThanhToan)

        MessageBox.Show("da su ly moi ban kiem tra lai tien no", "Thong Bao")

    End Sub
End Class